package com.maxd.service.selfCoupon;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.SelfCoupon;
import com.maxd.model.SelfCouponIssue;
import com.maxd.model.SelfCouponUser;
import com.maxd.respository.selfCoupon.SelfCouponIssueJpaRepository;
import com.maxd.respository.selfCoupon.SelfCouponJpaRepository;
import com.maxd.respository.selfCoupon.SelfCouponUserJpaRepository;
import com.maxd.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SelfCouponIssueServiceImpl implements SelfCouponIssueService {
    @Autowired
    private SelfCouponIssueJpaRepository jpaRepository;
    @Autowired
    private SelfCouponUserJpaRepository couponUserJpaRepository;
    @Autowired
    private SelfCouponJpaRepository couponJpaRepository;

    @Override
    public Result findAll(Integer page, Integer size, Long storeId) {
        Pageable pageable = PageRequest.of(page, size, Sort.by(new Sort.Order(Sort.Direction.DESC, "createTime")));
        Specification<SelfCouponIssue> queryCondition = new Specification<SelfCouponIssue>() {
            @Override
            public Predicate toPredicate(Root<SelfCouponIssue> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (storeId != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("storeId"), storeId));
                }else {
                    predicateList.add(criteriaBuilder.isNull(root.get("storeId")));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return ResultUtil.success(jpaRepository.findAll(queryCondition, pageable));
    }

    /**
     * 用户可领取的优惠券列表
     * @param goodsId
     * @param userId
     * @return
     */
    @Override
    public Result useList(String goodsId, Long userId) {
        List<SelfCouponIssue> list = jpaRepository.selectSelfCouponIssueList(goodsId==null?"":goodsId, userId);
        for (SelfCouponIssue c : list) {
            SelfCoupon coupon=new SelfCoupon();
            coupon.setCouponName(c.getCouponName());
            coupon.setGoodsImages(c.getGoodsImages());
            coupon.setLessMoney(c.getLessMoney());
            coupon.setMinMoney(c.getMinMoney());
            c.setCoupon(coupon);
            c.setGetStatus(1);
        }
        return ResultUtil.success(list);
    }

    @Override
    public Result saveBody(SelfCouponIssue entity) {
        SelfCoupon c = couponJpaRepository.findById(entity.getCouponId()).orElse(null);
        entity.setCouponName(c.getCouponName());
        entity.setGoodsImages(c.getGoodsImages());
        entity.setLessMoney(c.getLessMoney());
        entity.setMinMoney(c.getMinMoney());
        entity.setCreateTime(DateUtil.createTime());
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result updateBody(SelfCouponIssue entity) {
        return ResultUtil.success(jpaRepository.save(entity));
    }

    @Override
    public Result findOne(Long id) {
        return ResultUtil.success(jpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result delete(Long id) {
        jpaRepository.deleteById(id);
        return ResultUtil.success();
    }
}
